<?php

class bbdd{
	
	private $db_host;
	private $db_user_admin;
	private $db_pass_admin;
	private $db_name;
	private $db_conexion;
	
	public function __construct($db_host, $db_user_admin, $db_pass_admin, $db_name){
		$this->db_host = $db_host;
		$this->db_user_admin = $db_user_admin;
		$this->db_pass_admin = $db_pass_admin;
		$this->db_name = $db_name;
		$this->db_conexion = FALSE;
	}
	
	public function creaConexion(){
		$this->db_conexion = mysql_connect($this->db_host, $this->db_user_admin, $this->db_pass_admin); // or die('CONECTION REFUSSED <BR>');
	}
	
	public function getConexion(){
		return $this->db_conexion;
	}
	
	public function creaDB() {
		$sql = mysql_query("CREATE DATABASE ".$this->db_name);
		return $sql;
	}
	
	public function getNombreDB(){
		return $this->db_name;
	}
	
	public function ejecutaConsulta($query){
		return mysql_query($query); //SI LA CONSULTA DA ERROR DEVUELVE FALSE
	}
	
	public function capturaError(){
		return mysql_errno();
	}
	
	public function muestraError(){
		return mysql_error();
	}
	
	public function obtieneIdInsert(){
		return mysql_insert_id($this->db_conexion);
	}
	
	public function seleccionaBD() {
		return $db_selected = mysql_select_db($this->db_name, $this->db_conexion);
	}
	
	public function cierraConexion(){
		mysql_close($this->db_conexion);
	}
	
	public function libera($result){
		mysql_free_result($result);
	}
	
	public function recuperaValoresConsulta($result){
		return mysql_fetch_assoc($result);
	}
	
	public function instalaDB(){
		// Initialise variables.
		$return = true;
		
		// Get the contents of the schema file.
		if (!($contenido = file_get_contents("../include/install.sql"))) {
			return FALSE;
		}
		
		// Get an array of queries from the schema and process them.
		$queries = $this->_splitQueries($contenido);

		$this->seleccionaBD();
		
		foreach ($queries as $query)
		{
			// Trim any whitespace.
			$query = trim($query);

			// If the query isn't empty and is not a comment, execute it.
			if (!empty($query) && ($query{0} != '#')) {
				
				$this->ejecutaConsulta($query);
			}
		}
		
		return $return;	
	}
	
	//REVISAR
	function _splitQueries($sql)
	{
		
		$buffer		= array();
		$queries	= array();
		$in_string	= false;

		// eliminar espacios en blanco
		$sql = trim($sql);

		// eliminar comentarios
		$sql = preg_replace("/\n\#[^\n]*/", '', "\n".$sql);

		// Separar consultas
		for ($i = 0; $i < strlen($sql) - 1; $i ++)
		{
			if ($sql[$i] == ";" && !$in_string) {
				$queries[] = substr($sql, 0, $i);
				$sql = substr($sql, $i +1);
				$i = 0;
			}

			if ($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\") {
				$in_string = false;
			}
			elseif (!$in_string && ($sql[$i] == '"' || $sql[$i] == "'") && (!isset ($buffer[0]) || $buffer[0] != "\\")) {
				$in_string = $sql[$i];
			}
			if (isset ($buffer[1])) {
				$buffer[0] = $buffer[1];
			}
			$buffer[1] = $sql[$i];
		}

		// If the is anything left over, add it to the queries.
		if (!empty($sql)) {
			$queries[] = $sql;
		}
		return $queries;
	}
	
	
}

?>

